<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace api\wxapp\controller;
use cmf\controller\PublicController;
use cmf\controller\RestBaseController;
use wxapp\aes\WXBizDataCrypt;
use api\common\service\CurlService;
use api\common\Css\Css;
use api\common\model\RlEtProductXcxModel;
use api\common\model\RlEtAzdModel;
use api\common\model\RlEtCompanyModel;
use api\common\model\RlEtCustomerModel;
use api\common\model\RlEtAzsModel;
use api\common\model\RlEtJgModel;
use api\common\model\RlCompanyModel;
use api\common\model\RlZsmsUserQueryModel;
use api\common\model\RlEtCustInstalRelModel;
use api\common\model\RlHiseServiceProjectModel;
use think\Validate;
use api\common\service\FunctionService;
use think\Db;
use api\common\model\RlHiseAreaModel;
use api\common\model\RlHiseQxTableModel;
use api\common\model\RlHiseDqTableModel;
use api\common\service\MsgService;
use api\common\model\RlBmNodeRelModel;
class ItemController extends PublicController
{
    protected $install_status = [
        0 => '未进场',
        1 => '已进场',
        2 => '隐蔽施工中',
        3 => '隐蔽施工已结束',
        4 => '调试',
        5 => '运行',
        'X' => '作废',
    ];

    protected $kqshzt_type_status = [
        0 => '',//不跨区
        1 => '初始',
        2 => '提交',
        3 => '驳回',
        4 => '接受',
    ];
    protected $kqshzt_status = [
        '不跨区' => 0,
        '初始' => 1,
        '提交' => 2,
        '驳回'   => 3,
        '接受'   => 4,
    ];
    //手机号查询sms报备信息
    //sms正式系统调用地址：SMS正式服务器 三台 http://10.30.5.40:8020/ http://10.30.5.41:8020/ http://10.30.5.121:8020/
    const SMS_API_CS = 'http://172.16.45.208:8020/SMS/SMSExternalInterface/ZSMS_BAOBEI_QUERY';  //测试
    const SMS_API = 'http://sms.hisensehitachi.cn/SMS/SMSExternalInterface/ZSMS_BAOBEI_QUERY';  
    protected $auth = '*';
    protected function initialize()
    {
        if (empty($this->user)) {
            $this->error(['code' => 10001, 'msg' => '登录已失效!']);
        }
    	$this->Function = new FunctionService;
    	$this->rlHiseServiceProjectModel = new RlHiseServiceProjectModel;
    	$this->rlEtAzdModel = new RlEtAzdModel;
        $this->RlEtAzsModel = new RlEtAzsModel;
    	$this->RlEtCustomerModel = new RlEtCustomerModel;
        $this->RlEtCustInstalRelModel = new RlEtCustInstalRelModel;
        $this->RlEtCompanyModel = new RlEtCompanyModel;
        $this->RlCompanyModel = new RlCompanyModel;
        $this->RlZsmsUserQueryModel = new RlZsmsUserQueryModel;
    	$this->RlEtJgModel = new RlEtJgModel;
    }
    // 项目创建页面
    public function index()
    {



    }

    // 通过手机号获取报备信息
    public function getbaobei()
    {

    	$phone = input('param.phone');
    	$vip_no = input('param.vip_no');
        $rlHiseAreaModel = new RlHiseAreaModel();
        $rlHiseDqTableModel = new RlHiseDqTableModel();
        $rlHiseQxTableModel = new RlHiseQxTableModel();
    	if($phone)
    	{
            $jxsbm = $this->user['jxs_code'];
            $data['JXSBM'] = $jxsbm;
            $data['LXFS'] = $phone;
            if(config('is_ceshi'))
            {
                $result = CurlService::curlRequest(self::SMS_API_CS, $data);
            }else{
                $result = CurlService::curlRequest(self::SMS_API, $data);
            }
                    $result = json_decode($result, true);
                    $result['projectVoList'] = $this->Function->arraykeyToLower($result['projectVoList']);
                     //所在地区
                if($result['projectVoList'])
                {
                 foreach ($result['projectVoList'] as $key => $value) {
                    $result['projectVoList'][$key]['sf_name'] = $rlHiseAreaModel->where('sf_code',$result['projectVoList'][$key]['area_code'])->value('sf_name')?$rlHiseAreaModel->where('sf_code',$result['projectVoList'][$key]['area_code'])->value('sf_name'):'';
                    $result['projectVoList'][$key]['sf_code'] = $result['projectVoList'][$key]['area_code'];
                    $result['projectVoList'][$key]['dq_name'] = $rlHiseDqTableModel->where('dq_code',$result['projectVoList'][$key]['dq_code'])->value('dq_name');
                    $result['projectVoList'][$key]['qx_name'] = $rlHiseQxTableModel->where('qx_code',$result['projectVoList'][$key]['qx_code'])->value('qx_name');
                    }
                }
	    	if($result['TYPE'] == 'S')
	    	{
		    	$this->success("查询成功!", ['result' => $result]);
	    	}else{
	    		$this->error('查询失败');
	    	}
    	}
    	if($vip_no)
    	{
	    	$css = new Css();
	    	$result = $css->getDevice($vip_no);
	    	if($result['flag'] == 1)
	    	{
                $result['data'] = $this->Function->arraykeyToLower($result['data']);
                 //所在地区
                 foreach ($result['data'] as $key => $value) {
                    $result['data'][$key]['sf_code'] = $rlHiseAreaModel->where('sf_name',$result['data'][$key]['province_name'])->value('sf_code');
                    $result['data'][$key]['dq_code'] = $rlHiseDqTableModel->where('dq_name',$result['data'][$key]['city_name'])->value('dq_code');
                    $result['data'][$key]['qx_code'] = $rlHiseQxTableModel->where('qx_name',$result['data'][$key]['region_name'])->value('qx_code');
                 }
		    	$this->success("查询成功!", ['result' => $result]);
	    	}elseif($result['flag'] == 0){
	    		$this->error('查询失败');
	    	}elseif($result['flag'] == 2){
	    		$this->error('安全验证失败');
	    	}
    	}

    }
    // 获取产品系列
    public function productxcx()
    {
            $product_brand = input('param.product_brand');
    	$RlEtProductXcxModel = new RlEtProductXcxModel();
    	$result = $RlEtProductXcxModel->select()->toArray();
    	$this->success("查询成功!", ['result' => $result]);
    }
   // 获取产品系列试点
	public function productxcxsd()
	{
    	$product_brand = input('param.product_brand');
    	$RlEtProductXcxModel = new RlEtProductXcxModel();
    	$where['product_id']=array('in','57,67,68,69,70,71');
    	$result = $RlEtProductXcxModel->where($where)->select()->toArray();
    	$this->success("查询成功!", ['result' => $result]);
	}
    //获取全部地区信息
    public function areaall()
    {
        $result = $this->Function->areaall();
        $this->success("查询成功!", ['result' => $result]);
    }

    // //获取省信息
    public function getarea()
    {
    	$result = $this->Function->area();
    	$this->success("查询成功!", ['result' => $result]);
    }

    // //获取地区信息
    public function getdqtable()
    {
    	$sf_code = input('param.sf_code');
            if(!$sf_code)
                {
                    $this->error("请输入省份编码!");
                }
    	$result = $this->Function->dqtable($sf_code);
    	$this->success("查询成功!", ['result' => $result]);
    }

    // //获取区县信息
    public function getqxtable()
    {
    	$dq_code = input('param.dq_code');
            if(!$dq_code)
            {
                $this->error("请输入地区编码!");
            }
    	$result = $this->Function->qxtable($dq_code);
    	$this->success("查询成功!", ['result' => $result]);
    }

    //获取安装队信息
    public function getazd()
    {
        $jxsbm = $this->user['jxs_code'];
    	// $jxsbm = 2007487;
        if(!$jxsbm)
        {
            $this->error("请登录!");
        }
        $where =[];
        $keyword = input('param.keyword');
           if($keyword)
            {
                $where['engineer_name|phone'] = ['like','%'.$keyword.'%'];
            }
    	//获取安装商编码数组
    	$azss = $this->RlEtCustInstalRelModel->where('jxsbm',$jxsbm)->where('flag',1)->column('server_code');
    	$result = [];
    	if($azss)
    	{
    		$result = $this->rlEtAzdModel->where('server_code','in',$azss)->where($where)->where('active_flag',1)->select()->toArray();
    	}
    	$this->success("查询成功!", ['result' => $result]);
    }

    //安装商获取安装队长
    //获取安装队信息
    public function Azsgetazd()
    {
        $server_code = $this->user['server_code'];
        // $jxsbm = 2007487;
        if(!$server_code)
        {
            $this->error("请登录!");
        }
        $where =[];
        $keyword = input('param.keyword');
        if($keyword)
        {
            $where['engineer_name|phone'] = ['like','%'.$keyword.'%'];
        }
        //获取安装商队长
        $result = [];
        if($server_code)
        {
            $result = $this->rlEtAzdModel->where('server_code','in',$server_code)->where($where)->where('active_flag',1)->where("post",2)->select()->toArray();
        }
        $this->success("查询成功!", ['result' => $result]);
    }

        //获取经销商，分工司信息
    public function getuserrel()
    {
        $jxsbm = $this->user['jxs_code'];

        // $jxsbm = 2007487;
        if(!$jxsbm)
        {
            $this->error("请登录!");
        }
        //获取经销商信息
        $result['jxs'] = $this->RlEtCustomerModel->where('jxsbm',$jxsbm)->find();
        //查询分工司编码
        $fgs = $this->RlEtCompanyModel->where('jxsbm',$jxsbm)->value('fgs');
        $result['fgs'] = $this->RlCompanyModel->where('orgdept',$fgs)->find();
        $result['xs'] = $this->user;
        $result['fgsfwfzr'] = $this->RlZsmsUserQueryModel->where('bm_code',$fgs)->where('ignore',0)->where('gw','in','5,3')->select();
        $this->success("查询成功!", ['result' => $result]);
    }

     //获取安装商信息
    public function getazs()
    {
        $server_code = input('param.server_code');
        if(!$server_code)
        {
            $this->error("请输入安装商编号!");
        }
        //获取安装商信息
        $result = $this->RlEtAzsModel->where('server_code',$server_code)->find();
        //获取分公司信息
        $result['fgs'] = $this->RlCompanyModel->where('orgdept',$result['dept_name'])->find();
        //服务负责人
        $result['fgsfwfzr'] = $this->RlZsmsUserQueryModel->where('bm_code',$result['dept_name'])->where('ignore',0)->where('gw','in','5,3')->select();
        $this->success("查询成功!", ['result' => $result]);
    }

    //congmeijian获取安装商列表
    public function getazslist()
    {
        $jxsbm = $this->user['jxs_code'];
        // $jxsbm = 2007487;
        if(!$jxsbm)
        {
            $this->error("请登录!");
        }
        $where =[];
        $keyword = input('param.keyword');
        if($keyword)
        {
            $where['server_code|server_name'] = ['like','%'.$keyword.'%'];
        }
        //获取安装商信息
       $azss = $this->RlEtCustInstalRelModel->where('jxsbm',$jxsbm)->where('flag',1)->column('server_code');
        $result = $this->RlEtAzsModel->where('server_code','in',$azss)->where($where)->where('active_flag',1)->select()->toArray();
       // $result = $this->RlEtAzsModel->where('sap_server_code',$jxsbm)->where($where)->where('active_flag',1)->select();
        //获取分公司信息
       // $result['fgs'] = $this->RlCompanyModel->where('orgdept',$result['dept_name'])->find();
        //服务负责人
       // $result['fgsfwfzr'] = $this->RlZsmsUserQueryModel->where('bm_code',$result['dept_name'])->where('ignore',0)->where('gw','in','5,3')->select();
        $this->success("查询成功!", ['result' => $result]);
    }


    //获取项目信息1-待安装商派工，2-待安装队接受，3-安装队已接受，4-安装已退回，5-安装队已退回zzdpdzt
    public function getitem()
    {
    	$xsy_sj = $this->user['mobilphone'];
            $jxsbm = $this->user['jxs_code'];
           $zzdpdzt = input('param.zzdpdzt');
           $sfwc = input('param.sfwc');
           //分页
           $pege = input('param.pege');
           $limit = input('param.limit');
           if(!$pege || !$limit )
            {
                $this->error("请输入完整条件!");
            }
            if(!$sfwc && !$zzdpdzt )
            {
                $this->error("请输入完整条件!");
            }
            $where = [];
            if($sfwc)
            {
                if($sfwc==2)
                {
                    $where['gczt'] = 5;//已完成
                }else{
                    $where['gczt'] = ['in','0,1,2,3,4'];//进行中
                    $where['zzdpdzt'] = 3;
                }
            }elseif($zzdpdzt)
            {
                if($zzdpdzt == 2)
                {
                    $where['zzdpdzt'] = 5;//5已退回,
                    $where['gczt'] = ['<>','X'];
                }else{
                    $where['zzdpdzt'] = ['in','0,2'];//2待安装队接受，0就是编辑了一半
                    $where['gczt'] = ['<>','X'];
                }
            }
           $start = ($pege-1)*$limit;
           $selectdata = $this->request->param();
        $funt = new FunctionService;
        $selectazd = $funt->selectazd($selectdata);
       // $where = array_merge($where,$selectazd);
        $wheres='';
        $is_apply_off=input('param.is_apply_off');
        if(isset($is_apply_off)){
            $wheres='ycgcwcsj+(2592000*(yqsqzq-1)) <'.time();
        }
    	//获取项目信息数组
    	$result = $this->rlHiseServiceProjectModel
            ->where('xsy_sj',$xsy_sj)
            ->where('jxs',$jxsbm)
            ->where($where)
            ->where($wheres)
            ->where(
                function ($result)use($selectdata){
                    foreach($selectdata as $k=>$v){
                        switch ($k){
                            case 'yh_name':
                                $result->whereOr('yh_name','like','%'.$v.'%');
                                break;
                            case 'sj_one':
                                $result->whereOr('sj_one','like','%'.$v.'%');
                                break;
                            case 'address':
                                $result->whereOr('address','like','%'.$v.'%');
                                break;
                            case 'jkh':
                                $result->whereOr('jkh','like','%'.$v.'%');
                                break;
                            case 'jxsxsy':
                                $result->whereOr('jxsxsy','like','%'.$v.'%');
                                break;
                            case 'xsy_sj':
                                $result->whereOr('xsy_sj','like','%'.$v.'%');
                                break;
                            case 'engineer_name':
                                $azdwhere['engineer_name'] = ['like','%'.$v.'%'];
                                $engineer_code = Db::name('rl_et_azd')->where($azdwhere)->column('engineer_code');
                                $result->whereOr('azd','in',$engineer_code);
                                break;
                            case 'azdphone':
                                $result->whereOr('azd_sj','like','%'.$v.'%');
                                break;
                           case 'gczt':
                                $result->whereOr('gczt',$v);
                              break;
                            default:
                                break;
                        }
                    }
                }
            )
            ->order('create_time', 'desc')
            ->limit($start,$limit)
            ->select()
            ->toArray();
        foreach ($result as $key => $value) {
            $result[$key]['create_time']= date('Y-m-d',$result[$key]['create_time']);
           if($value['azd']){
			$result[$key]['azdmz']= $this->rlEtAzdModel->where('engineer_code',$value['azd'])->value('engineer_name');
		}else{
			$result[$key]['azdmz']= '';
		}
            //$result[$key]['azdmz']= $this->rlEtAzdModel->where('engineer_code',$value['azd'])->value('engineer_name');
            $result[$key]['problem_num']  = Db::name('rl_inspect')->where('azd_id',$value['azd_id'])->where('is_problem',1)->where('inspect_status',2)->count();
            $result[$key]['gczt_name'] = $this->install_status[$value['gczt']];
            $h=date("H",$value['yyaz_sj']);
        if($h<12)  $result[$key]['date'] = "上午";
        else if($h<19) $result[$key]['date'] = "下午";
        $result[$key]['yyaz_sj']= date('Y-m-d',$result[$key]['yyaz_sj']);

        }
           $jscount = $this->rlHiseServiceProjectModel->where('xsy_sj',$xsy_sj)->where('jxs',$jxsbm)->where('zzdpdzt',5)->where('gczt','<>','X')->count();//已退回
           $bjcount = $this->rlHiseServiceProjectModel->where('xsy_sj',$xsy_sj)->where('jxs',$jxsbm)->where('zzdpdzt','in','0,2')->where('gczt','<>','X')->count();//待接收，编辑中
           $jxcount = $this->rlHiseServiceProjectModel->where('xsy_sj',$xsy_sj)->where('jxs',$jxsbm)->where('gczt','in','0,1,2,3,4')->where('zzdpdzt',3)->count();//进行中
           $wccount = $this->rlHiseServiceProjectModel->where('xsy_sj',$xsy_sj)->where('jxs',$jxsbm)->where('gczt',5)->count();//完成
    	$this->success("查询成功!", ['result' => $result,'jscount'=>$jscount,'jxcount'=>$jxcount,'wccount'=>$wccount,'bjcount'=>$bjcount]);
    }

     //获取项目详细信息
    public function getitemdetail()
    {
           $hise_service_project_id = input('param.hise_service_project_id');
        //获取项目信息数组
        $result = $this->rlHiseServiceProjectModel->where('hise_service_project_id',$hise_service_project_id)->find();
        $data_time = $result['yyaz_sj'];
        $h=date("H",$data_time);
        if($h<12)  $result['date'] = "上午";
        else if($h<19) $result['date'] = "下午";
        $result['yyaz_sj']= date('Y-m-d',$data_time);

        //所在地区
        $rlHiseAreaModel = new RlHiseAreaModel();
        $rlHiseDqTableModel = new RlHiseDqTableModel();
        $rlHiseQxTableModel = new RlHiseQxTableModel();
        $result['sf_name'] = $rlHiseAreaModel->where('sf_code',$result['provice'])->value('sf_name');
        $result['dq_name'] = $rlHiseDqTableModel->where('dq_code',$result['city'])->value('dq_name');
        $result['qx_name'] = $rlHiseQxTableModel->where('qx_code',$result['qx'])->value('qx_name');
        //分公司
        $result['fgsmz'] = $this->RlCompanyModel->where('orgdept',$result['qyfgs'])->find();
        $result['fwfgs_mz'] = $this->RlCompanyModel->where('orgdept',$result['fwfgs'])->find();
        //经销商
        $result['jxsmz'] = $this->RlEtCustomerModel->where('jxsbm',$result['jxs'])->value('jxs_mc');
        //安装队
        $result['azdmz']= $this->rlEtAzdModel->where('engineer_code',$result['azd'])->value('engineer_name');
        //安装商
        $result['azsmz'] = $this->RlEtAzsModel->where('server_code',$result['azs'])->find();
        //跨区审核状态
        $result['kqshzt_name'] = $this->kqshzt_type_status[$result['kqshzt']];
        //产品系列
        $cplb = explode("/", $result['cplb']);
        $RlEtProductXcxModel = new RlEtProductXcxModel();
        $product = $RlEtProductXcxModel->where('product_id','in',$cplb)->select()->toArray();
        $result['cplbmz'] = '';
        foreach ($product as $key => $value) {
            $result['cplbmz'] .= $value['product_name'];
        }

        $this->success("查询成功!", ['result' => $result]);
    }

    //保存业主信息
    public function createitem()
    {
    	$validate = new Validate([
            'yh_name' => 'require',
            'xb' => 'require',
            'sj_one' => 'require',
            // 'sj_two' => 'number',
            // 'gddh' => 'number',
            'provice' => 'require',
            'city' => 'require',
            'qx' => 'require',
            // 'address' => 'require',
            // 'cplb' => 'require',
            'zjbxq' => 'require',
            'zybjbxq' => 'require',
        ]);

        $validate->message([
            'yh_name' => '请填写姓名',
            'xb' => '请选择性别',
            'sj_one' => '请填写手机号',
            // 'sj_two' => '请填写备用手机号',
            // 'gddh' => '请填写固定电话',
            'provice' => '请选择所在省',
            'city' => '请选择所在市',
            'qx' => '请选择所在区',
            // 'address' => '请填写详细地址',
            // 'cplb' => '请选择产品系列',
            'zjbxq' => '请填写整机保修期',
            'zybjbxq' => '请填写主要零部件保修期',

        ]);
        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
        if(!$data['cplb'])
        {
            $this->error('请选择产品系列');
        }
        if(!$data['address'])
        {
            $this->error('请填写详细地址');
        }
     //判断人员是否有部门信息
        if(!$this->user['bm_code']){
            $this->error('没有部门编号，请联系系统管理员');
        }

        $data_n['xsy_sj'] = $this->user['mobilphone'];
        $data_n['jxsxsy'] = $this->user['user_name'];
        $data_n['jxs'] = $this->user['jxs_code'];
        $data_n['update_time'] = time();
        $data_n['yh_name'] = $data['yh_name'];
        $data_n['xb'] = $data['xb'];
        $data_n['sj_one'] = $data['sj_one'];
        $data_n['gddh'] = $data['gddh'];
        $data_n['provice'] = $data['provice'];
        $data_n['city'] = $data['city'];
        $data_n['qx'] = $data['qx'];
        $data_n['address'] = $data['address'];
        $data_n['sj_two'] = $data['sj_two'];
        $data_n['cplb'] = $data['cplb'];
        $data_n['zjbxq'] = $data['zjbxq'];
        $data_n['pic_one'] = $data['pic_one'];
        $data_n['pic_two'] = $data['pic_two'];
        $data_n['zybjbxq'] = $data['zybjbxq'];
        $data_n['kqshzt'] = 0;
        $data_n['sfkq_bs'] = $data['sfkq_bs'];
        if($data_n['sfkq_bs'])
        {
            $data_n['kqshzt'] = $this->kqshzt_status['初始'];
            //获取签约分公司编码
            $fgs = $this->RlEtCompanyModel->where('jxsbm',$this->user['jxs_code'])->value('fgs');
            $data_n['qyfgs'] = $fgs;
        }
        $data_n['sfkaz_bs'] = 0;
        $data_n['gcpp'] = 1;
        if(isset($data['jkh']) && $data['jkh']){
            if($data['jkh'] >= config('installsendstar') && $data['jkh'] <= config('installsendend'))
            {
                
            }elseif(substr($data['jkh'],0,3)=='HXT'&&strlen($data['jkh'])==9&&is_numeric(substr($data['jkh'],3))){
                if(substr($data['jkh'],3)>200000)
                {
                    $this->error('VIP卡号范围不正确');
                }
            }else{
                $this->error('VIP卡号格式不正确');
            }
        }
        $data_n['jkh'] = isset($data['jkh'])?$data['jkh']:'';
        $data_n['bbdh'] = isset($data['bbdh'])?$data['bbdh']:'';
        //判断报备单是否是重复使用
        if($data_n['bbdh'] && !$data['id']){
            $is_bbdh = $this->rlHiseServiceProjectModel->where('bbdh',$data_n['bbdh'])->find();
            if($is_bbdh)
            {
                $this->error('此报备单号已使用');
            }
        }
        //所在地区
        $rlHiseAreaModel = new RlHiseAreaModel();
        $rlHiseDqTableModel = new RlHiseDqTableModel();
        $rlHiseQxTableModel = new RlHiseQxTableModel();
        $sf_name = $rlHiseAreaModel->where('sf_code',$data['provice'])->value('sf_name');
        $dq_name = $rlHiseDqTableModel->where('dq_code',$data['city'])->value('dq_name');
        $qx_name = $rlHiseQxTableModel->where('qx_code',$data['qx'])->value('qx_name');
        $data_n['azd_name'] = $sf_name.$dq_name.$qx_name.$data['address'];
        
        //生成安装单编号
        if(isset($data['azd_id']) && $data['azd_id']){
            $data_n['azd_id'] = $data['azd_id'];
            $azd_id = $data['azd_id'];
        }else{
            if(config('is_ceshi'))
            {
                $azd_id = 'CSXCX'.date('YmdHis',time()).rand(1111,9999);
            }else{
                $azd_id = 'JZXCX'.date('YmdHis',time()).rand(1111,9999);
            }
            $data_n['azd_id'] = $azd_id;
        }
        $data_n['azd_id'] = $azd_id;
        
        if(isset($data['id']) && $data['id']){
            $hise_service_project_id = $data['id'];
            unset($data['id']);
                    $rusult = $this->rlHiseServiceProjectModel->where('hise_service_project_id',$hise_service_project_id)->update($data_n);
                    if($rusult)
                    {
                        $rusult = $hise_service_project_id;
                    }
        }else{
                    $data_n['create_time'] = time();
	        $rusult = $this->rlHiseServiceProjectModel->insertGetId($data_n);
        }
        if($rusult)
        {
        	$this->success("保存成功!",['id'=>$rusult]);
        }else{
        	$this->error("保存失败!");
        }
    }

    //保存工程信息
    public function updateitem()
    {
    	$validate = new Validate([
            'yyaz_sj' => 'require',
            'fwfgs' => 'require',
            // 'qyfgs' => 'require',
            'jxs' => 'require',
            'azs' => 'require',
           // 'azd' => 'require',
            'jxsxsy' => 'require',
            'fgsfwfzr' => 'require',
            'fgsfwfzr' => 'require',
            'id' => 'require',
        ]);

        $validate->message([
            'yyaz_sj' => '请填写预约安装时间',
            'fwfgs' => '请填写服务分公司',
            // 'qyfgs' => '请填写签约分公司',
            'jxs' => '请填写经销商',
            'azs' => '请填写安装商',
           // 'azd' => '请填写安装队',
            'jxsxsy' => '请填写经销商销售员',
            'fgsfwfzr' => '请填写分公司服务负责人',
            'id' => '请填写项目id',

        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
        $data_n['yyaz_sj'] = $data['yyaz_sj']/1000;
        $data_n['fwfgs'] = $data['fwfgs'];
        $data_n['jxs'] = $data['jxs'];
        $data_n['azs'] = $data['azs'];
        $data_n['azd'] = $data['azd'];
        $data_n['jxsxsy'] = $data['jxsxsy'];
        $data_n['fgsfwfzr'] = $data['fgsfwfzr'];
        $data_n['update_time'] = time();
        $data_n['qyfgs'] = $data['fwfgs'];
        $data_n['bz'] = $data['bz'];
        $data_n['zzdpdzt'] = 2;
        $hise_service_project_id = $data['id'];
        /*******2020.9.8start修改节点规则，一旦选定分类就固定为当时的节点，不会变化*******/
        $where = $data['jxs'].','.$data['fwfgs'].'.'.$data['jxs'].','.'1';
        $list = RlBmNodeRelModel::where('bm','in',$where)->field('node_id')->select();

        $str = '';
        foreach ($list as $k =>$v){
            if($k==0){
                $str = $str.$v['node_id'];
            }else{
                $str = $str.','.$v['node_id'];
            }
        }
        $data_n['node_ids'] = $str;
        /*******2020.9.8end修改节点规则，一旦选定分类就固定为当时的节点，不会变化*******/
        //判断是否精工
        $jg = $this->RlEtJgModel->where('server_code',$data['azs'])->where('qs_date','<=',date('H',time()))->where('jz_date','>=',date('H',time()))->find();
        if($jg)
        {
            $data_n['sfjg'] = 1;
        }else{
            $data_n['sfjg'] = 0;
        }
        unset($data['id']);
        $azd_result = $this->rlHiseServiceProjectModel->where('hise_service_project_id',$hise_service_project_id)->value('azd');
        if($azd_result)
        {
            $this->success("已派工，不允许修改!");
        }
        $rusult = $this->rlHiseServiceProjectModel->where('hise_service_project_id',$hise_service_project_id)->update($data_n);
        if($rusult)
        {
            if(config('installsend1'))
          {
            //获取安装队手机号信息
            $phone = $this->rlEtAzdModel->where('engineer_code',$data_n['azd'])->find()['phone'];
            $azd_id = $this->rlHiseServiceProjectModel->where('hise_service_project_id',$hise_service_project_id)->find()['azd_id'];
            // 引用发送验证码类
                $msg_send = new MsgService();
                // 发送手机验证码
                $msgres = $msg_send->installsend1($azd_id,$phone);
                if($msgres)
                {
                    trace($phone.'||'.date('Y-m-d h:i:s',time()).'installsend1短信发送成功','info');
                }else{
                    trace($phone.'||'.date('Y-m-d h:i:s',time()).'installsend1短信发送失败','info');
                }
          }
        	$this->success("保存成功!");
        }else{
        	$this->error("保存失败!");
        }
    }
    
    //分公司列表
    public function fgslist()
    {
        $result = $this->RlCompanyModel->where('active_flag',1)->where('orgtype',5)->select()->toArray();
        $this->success("查询成功!", ['result' => $result]);
    }
    //跨区单子选择服务分公司
    public function setfwfgs()
    {
        $data = $this->request->param();
        $validate = new Validate([
            'id' => 'require',
            'fwfgs' => 'require',
        ]);

        $validate->message([
            'id' => '请填写项目id',
            'fwfgs' => '请填写服务分公司',
        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
        $hise_service_project_id = $data['id'];
        unset($data['id']);
        $data_n['fwfgs'] = $data['fwfgs'];
        $data_n['kqshzt'] = 2;
        //添加申请跨区记录
        $kq['azd_id'] = $this->rlHiseServiceProjectModel->where('hise_service_project_id',$hise_service_project_id)->find()['azd_id'];
        $kq['applicant_id'] = $this->user['mobilphone'];
        $kq['applicant_name'] = $this->user['user_name'];
        $kq['applicant_time'] = time();
        $kq['status'] = 1;
        $kq_log = Db::name('azd_kq')->insertGetId($kq);
        $rusult = 0;
        if($kq_log)
        {

            $rusult = $this->rlHiseServiceProjectModel->where('hise_service_project_id',$hise_service_project_id)->update($data_n);
        }
        if($rusult){
            $this->success("分配成功!");
        }else{
            $this->error("分配失败!");
        }
    }

    //跨区单子接受或者驳回
    public function setonfwfgs()
    {
        $data = $this->request->param();
        $validate = new Validate([
            'id' => 'require',
            'kqshzt' => 'require',
        ]);

        $validate->message([
            'id' => '请填写项目id',
            'kqshzt' => '请填写接受状态',
        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
        $hise_service_project_id = $data['id'];
        unset($data['id']);
        $data_n['kqshzt'] = $data['kqshzt'];
        $data_n['update_time'] = time();
        
        $type_name = '接收';
        if($data['kqshzt']==3) $type_name = '驳回';
        //添加申请跨区记录
        $azd_id = $this->rlHiseServiceProjectModel->where('hise_service_project_id',$hise_service_project_id)->find()['azd_id'];
        $kq_id = Db::name('azd_kq')->where('azd_id',$azd_id)->where('status','in','1,2')->find()['id'];
        $result = 0;
        if($data['kqshzt']==3)
        {
            $kq['overrule_id'] = $this->user['mobilphone'];
            $kq['overrule_name'] = $this->user['user_name'];
            $kq['overrule_time'] = time();
            $kq['overrule_commet'] = $data['overrule_commet'];
            $kq['status'] = 3;
            $kq_log = Db::name('azd_kq')->where('id',$kq_id)->update($kq);
            if($kq_log)
            {

                $result = $this->rlHiseServiceProjectModel->where('hise_service_project_id',$hise_service_project_id)->update($data_n);
            }
        }else{
            $kq['examine_id'] = $this->user['mobilphone'];
            $kq['examine_name'] = $this->user['user_name'];
            $kq['examine_time'] = time();
            $kq['status'] = 2;
            $kq_log = Db::name('azd_kq')->where('id',$kq_id)->update($kq);
            if($kq_log)
            {
                $data_n['azs']      = $data['azs'];
                $data_n['azd']      = $data['azd'];
                $data_n['fgsfwfzr'] = $this->user['user_name'];
                $data_n['yyaz_sj'] = $data['yyaz_sj']/1000;
                $data_n['id']       = $hise_service_project_id;
                $azd_s = $this->rlHiseServiceProjectModel->where('hise_service_project_id',$hise_service_project_id)->find();
                $data_n['jxs']       = $azd_s['jxs'];
                $data_n['qyfgs']       = $azd_s['qyfgs'];
                $result = $this->kqeditazd($data_n);
            }
        }
        
    
        if($result){
            $this->success($type_name . "成功!");
        }else{
            $this->error($type_name . "失败!");
        }
    }

    //跨区项目获取安装队
    public function kqazd()
    {
        //获取分公司编码
        $hise_service_project_id = input('param.hise_service_project_id');
        $item = $this->rlHiseServiceProjectModel->where('hise_service_project_id',$hise_service_project_id)->field('fwfgs,qyfgs')->find();
        $fgs = $item['qyfgs'] . ',' . $item['fwfgs'];
        //获取安装商编码数组
        $azss = $this->RlEtAzsModel->where('dept_name','in',$fgs)->where('active_flag',1)->column('server_code');
        $result = [];
        if($azss)
        {
            $where = [];
            $keyword = input('param.keyword');
               if($keyword)
                {
                    $where['engineer_name|phone'] = ['like','%'.$keyword.'%'];
                }
            $result = $this->rlEtAzdModel->where('server_code','in',$azss)->where($where)->where('active_flag',1)->select()->toArray();
        }
        $this->success("查询成功!", ['result' => $result]);
    }
    //跨区项目获取安装商
    public function kqazs()
    {
        $result = [];
        //获取分公司编码
        $hise_service_project_id = input('param.hise_service_project_id');
        $item = $this->rlHiseServiceProjectModel->where('hise_service_project_id',$hise_service_project_id)->field('fwfgs,qyfgs')->find();
        $fgs = $item['qyfgs'] . ',' . $item['fwfgs'];
        //获取安装商编码数组
        $result = $this->RlEtAzsModel->where('dept_name','in',$fgs)->where('active_flag',1)->select()->toArray();
        $this->success("查询成功!", ['result' => $result]);
    }
    //跨区项目分派施工队
    public function kqeditazd($data)
    {
        $validate = new Validate([
            'azs' => 'require',
            'azd' => 'require',
            'id' => 'require',
            'fgsfwfzr' => 'require',
            'yyaz_sj' => 'require',
        ]);

        $validate->message([
            'azs' => '请填写安装商',
            'azd' => '请填写安装队',
            'id' => '请填写项目id',
            'fgsfwfzr' => '请填写分公司服务负责人',
            'yyaz_sj' => '请填写预约安装时间',
        ]);

        
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
        //判断是否精工
        $jg = $this->RlEtJgModel->where('server_code',$data['azs'])->where('qs_date','<=',date('H',time()))->where('jz_date','>=',date('H',time()))->find();
        if($jg)
        {
            $data['sfjg'] = 1;
        }else{
            $data['sfjg'] = 0;
        }
        $data['zzdpdzt'] = 2;
        $data['update_time'] = time();
        $hise_service_project_id = $data['id'];
        $where = $data['jxs'].','.$data['qyfgs'].'.'.$data['jxs'].','.'1';
        $list = RlBmNodeRelModel::where('bm','in',$where)->field('node_id')->select();

        $str = '';
        foreach ($list as $k =>$v){
            if($k==0){
                $str = $str.$v['node_id'];
            }else{
                $str = $str.','.$v['node_id'];
            }
        }
        $data['node_ids'] = $str;

        unset($data['id']);
        $rusult = $this->rlHiseServiceProjectModel->where('hise_service_project_id',$hise_service_project_id)->update($data);
        if($rusult)
        {
            if(config('installsend1'))
          {
            //获取安装队手机号信息
            $phone = $this->rlEtAzdModel->where('engineer_code',$data['azd'])->find()['phone'];
            $azd_id = $this->rlHiseServiceProjectModel->where('hise_service_project_id',$hise_service_project_id)->find()['azd_id'];
            // 引用发送验证码类
                $msg_send = new MsgService();
                // 发送手机验证码
                $msgres = $msg_send->installsend1($azd_id,$phone);
                if($msgres)
                {
                    trace($phone.'||'.date('Y-m-d h:i:s',time()).'installsend1短信发送成功','info');
                }else{
                    trace($phone.'||'.date('Y-m-d h:i:s',time()).'installsend1短信发送失败','info');
                }
          }
            return 1;
        }else{
            return 0;
        }
    }

    //再次分派施工队
    public function editazd()
    {
        $validate = new Validate([
            'azs' => 'require',
           // 'azd' => 'require',
            'id' => 'require',
            'fwfgs' => 'require',
            'qyfgs' => 'require',
            'fgsfwfzr' => 'require',
        ]);

        $validate->message([
            'azs' => '请填写安装商',
           // 'azd' => '请填写安装队',
            'id' => '请填写项目id',
            'fwfgs' => '请填写服务分公司',
            'qyfgs' => '请填写服务分公司',
            'fgsfwfzr' => '请填写分公司服务负责人',
        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
        //判断是否精工
        $jg = $this->RlEtJgModel->where('server_code',$data['azs'])->where('qs_date','<=',date('H',time()))->where('jz_date','>=',date('H',time()))->find();
        if($jg)
        {
            $data['sfjg'] = 1;
        }else{
            $data['sfjg'] = 0;
        }
        $data['zzdpdzt'] = 2;
        $data['update_time'] = time();
        $hise_service_project_id = $data['id'];
        unset($data['id']);
        $rusult = $this->rlHiseServiceProjectModel->where('hise_service_project_id',$hise_service_project_id)->update($data);
        if($rusult)
        {
            if(config('installsend1'))
          {
            //获取安装队手机号信息
            $phone = $this->rlEtAzdModel->where('engineer_code',$data['azd'])->find()['phone'];
            $azd_id = $this->rlHiseServiceProjectModel->where('hise_service_project_id',$hise_service_project_id)->find()['azd_id'];
            // 引用发送验证码类
                $msg_send = new MsgService();
                // 发送手机验证码
                $msgres = $msg_send->installsend1($azd_id,$phone);
                if($msgres)
                {
                    trace($phone.'||'.date('Y-m-d h:i:s',time()).'installsend1短信发送成功','info');
                }else{
                    trace($phone.'||'.date('Y-m-d h:i:s',time()).'installsend1短信发送失败','info');
                }
          }
            $this->success("分配成功!");
        }else{
            $this->error("分配失败!");
        }
    }
    //销售和经销商直接再次分派施工队，不用安装队接收
    public function twoeditazd()
    {
        $validate = new Validate([
            'azs' => 'require',
            'azd' => 'require',
            'id' => 'require',
            'fwfgs' => 'require',
            'qyfgs' => 'require',
            'fgsfwfzr' => 'require',
        ]);

        $validate->message([
            'azs' => '请填写安装商',
            'azd' => '请填写安装队',
            'id' => '请填写项目id',
            'fwfgs' => '请填写服务分公司',
            'qyfgs' => '请填写服务分公司',
            'fgsfwfzr' => '请填写分公司服务负责人',
        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
        //判断是否精工
        $jg = $this->RlEtJgModel->where('server_code',$data['azs'])->where('qs_date','<=',date('H',time()))->where('jz_date','>=',date('H',time()))->find();
        if($jg)
        {
            $data['sfjg'] = 1;
        }else{
            $data['sfjg'] = 0;
        }
        $data['update_time'] = time();
        $hise_service_project_id = $data['id'];
        unset($data['id']);
        $rusult = $this->rlHiseServiceProjectModel->where('hise_service_project_id',$hise_service_project_id)->update($data);
        if($rusult)
        {
            if(config('installsend1'))
          {
            //获取安装队手机号信息
            $phone = $this->rlEtAzdModel->where('engineer_code',$data['azd'])->find()['phone'];
            $azd_id = $this->rlHiseServiceProjectModel->where('hise_service_project_id',$hise_service_project_id)->find()['azd_id'];
            // 引用发送验证码类
                $msg_send = new MsgService();
                // 发送手机验证码
                $msgres = $msg_send->installsend1($azd_id,$phone);
                if($msgres)
                {
                    trace($phone.'||'.date('Y-m-d h:i:s',time()).'installsend1短信发送成功','info');
                }else{
                    trace($phone.'||'.date('Y-m-d h:i:s',time()).'installsend1短信发送失败','info');
                }
          }
            $this->success("分配成功!");
        }else{
            $this->error("分配失败!");
        }
    }


    //上传照片
    public function img()
    {
        $result = $this->Function->upload();
        return json($result);
    }

        //删除安装单
    public function delazd()
    {
        $validate = new Validate([
            'id' => 'require',
        ]);

        $validate->message([
            'id' => '请填写项目id',

        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
        $azd_id = $this->rlHiseServiceProjectModel->where('gczt','<',3)->where('hise_service_project_id',$data['id'])->find()['azd_id'];
        if(!$azd_id)
        {
            $this->error("安装单已经隐蔽结束，不能删除!");
        }
        if($this->user['gw']<3)
        {
            $azd_id = $this->rlHiseServiceProjectModel->where('gczt','<',1)->where('hise_service_project_id',$data['id'])->find()['azd_id'];
            if(!$azd_id)
            {
                $this->error("无删除权限!");
            }
        }
        $create_time = $this->rlHiseServiceProjectModel->where('hise_service_project_id',$data['id'])->find()['create_time'];
        if($create_time<strtotime('2020-05-09'))
        {
            $this->error("无删除权限!");
        }
        // 启动事务
        Db::startTrans();
        try{
            // 提交事务
            $this->rlHiseServiceProjectModel->where('gczt','<',3)->where('hise_service_project_id',$data['id'])->delete();
            Db::table('cmf_rl_hise_service_pro_cont_detail')->where('ht_azdbh',$azd_id)->delete();
            $zpdz = Db::table('cmf_rl_hise_service_pro_inst_detail')->where('hise_service_pro_inst_detail_id',$azd_id)->column('zpdz');
            foreach ($zpdz as $key => $value) {
                if($value)
                {
                    unlink(ROOT_PATH.'public'.$value);
                }
            }
            $process = Db::table('cmf_rl_hise_service_pro_process')->where('gc_azdid',$azd_id)->select()->toArray();
            foreach ($process as $key => $value) {
                if($value['tp_one'])
                {
                    unlink(ROOT_PATH.'public'.$value['tp_one']);
                }
                if($value['tp_two'])
                {
                    unlink(ROOT_PATH.'public'.$value['tp_two']);
                }
                if($value['tp_there'])
                {
                    unlink(ROOT_PATH.'public'.$value['tp_there']);
                }
            }
            Db::table('cmf_rl_hise_service_pro_process')->where('gc_azdid',$azd_id)->delete();
            Db::table('cmf_rl_hise_service_pro_inst_detail')->where('azd_id',$azd_id)->delete();


            $inspect_codes = Db::table('cmf_rl_inspect')->where('azd_id',$azd_id)->column('inspect_code');
            Db::table('cmf_rl_inspect')->where('inspect_code','in',$inspect_codes)->delete();
            Db::table('cmf_rl_inspect_score')->where('inspect_code','in',$inspect_codes)->delete();
            $step = Db::table('cmf_rl_inspect_step')->where('inspect_code','in',$inspect_codes)->select()->toArray();
            foreach ($step as $key => $value) {
                if($value['specs_pic'])
                {
                    unlink(ROOT_PATH.'public'.$value['specs_pic']);
                }
                if($value['specs_pic_two'])
                {
                    unlink(ROOT_PATH.'public'.$value['specs_pic_two']);
                }
                if($value['specs_pic_three'])
                {
                    unlink(ROOT_PATH.'public'.$value['specs_pic_three']);
                }
            }
            $step_checking = Db::table('cmf_rl_inspect_step_checking')->where('inspect_code','in',$inspect_codes)->select()->toArray();
            foreach ($step_checking as $key => $value) {
                if($value['pic_one'])
                {
                    unlink(ROOT_PATH.'public'.$value['pic_one']);
                }
                if($value['pic_two'])
                {
                    unlink(ROOT_PATH.'public'.$value['pic_two']);
                }
                if($value['pic_three'])
                {
                    unlink(ROOT_PATH.'public'.$value['pic_three']);
                }
                if($value['pic_four'])
                {
                    unlink(ROOT_PATH.'public'.$value['pic_four']);
                }
                if($value['pic_five'])
                {
                    unlink(ROOT_PATH.'public'.$value['pic_five']);
                }
            }
            Db::table('cmf_rl_inspect_step')->where('inspect_code','in',$inspect_codes)->delete();
            Db::table('cmf_rl_inspect_score_scoring')->where('inspect_code','in',$inspect_codes)->delete();
            Db::table('cmf_rl_inspect_step_checking')->where('inspect_code','in',$inspect_codes)->delete();
            Db::commit(); 
            $rusult = 1;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $rusult = 0;
        }
        if($rusult)
        {
            $this->success("删除成功!");
        }else{var_dump($e->getMessage());
            $this->error("删除失败!");
        }
    }

    //查询同安装商下的销售
    public function userlist()
    {
        
        $where = [];
        $param = $this->request->param();
        if(isset($param['keyword']) && $param['keyword'])
        {

            $where['user_name'] = ['like','%'.$param['keyword'].'%'];
        }
        if(isset($param['mobilphone']) && $param['mobilphone'])
        {

            $where['mobilphone'] = ['like','%'.$param['mobilphone'].'%'];
        }
        // if(isset($param['zyd_mc']) && $param['zyd_mc'])
        // {

        //     $where['zyd_mc'] = ['like','%'.$param['zyd_mc'].'%'];
        // }
        $where['zyd_mc'] = $this->user['zyd_mc'];
        $a = $this->RlZsmsUserQueryModel->where('bm_code',$this->user['bm_code'])->where('ignore',0)->where('status_sms','<>',2)->where($where)->column('zyd_mc');
        $zyd_mc = [];
        foreach($a as $k=>$v){
            if (!in_array($v,$zyd_mc)){
                $zyd_mc[]  = $v;
            }
        }
        $user = $this->RlZsmsUserQueryModel->where('bm_code',$this->user['bm_code'])->where('ignore',0)->where('status_sms','<>',2)->where($where)->order('zyd_mc', 'desc')->select()->toArray();
        $data = [];
        foreach ($zyd_mc as $key => $value) {
            $data[$key]['zyd_mc'] = $value;
            foreach ($user as $k => $v) {
                if($v['zyd_mc'] == $value)
                {
                    //经销商
        $v['jxs_mc'] = $this->RlEtCustomerModel->where('jxsbm',$this->user['bm_code'])->value('jxs_mc');
                    $data[$key]['user'][] = $v;
                }
            }
        }
        $this->success("查询成功!",['data'=>$data]);
    }

    //获取用户信息
    public function userinfo()
    {
        $user = $this->user;
        $where='ycgcwcsj+(2592000*(yqsqzq+9-1)) <'.time().' and gczt not in ("5","X")  and ycgcwcsj>0 and zzdpdzt=3 ';
        if(in_array($this->user['gw'], [0,1,2]))
        {
            $user['bm_mc'] = $this->RlEtCustomerModel->where('jxsbm',$this->user['bm_code'])->value('jxs_mc');
            $user['delay_num']=$this->rlHiseServiceProjectModel->where('ycgcsfjsbs',1)->where($where)->where('jxs',$this->user['bm_code'])->count();
        }elseif(in_array($this->user['gw'], [3,4,5,6,7,8,9,10])){
            $where='ycgcwcsj+(2592000*(yqsqzq+9-1)) <'.time().' and gczt not in ("5","X")  and ycgcwcsj>0 and zzdpdzt=3 and is_apply_delay=1';
            $user['bm_mc'] = $this->RlCompanyModel->where('orgdept',$this->user['bm_code'])->value('orgname');
            $list=$this->rlHiseServiceProjectModel->where('ycgcsfjsbs',1)->where($where)->where('fwfgs',$this->user['bm_code'])->select();
            if($list){
                foreach($list as $k=>$v){
                    if(!Db::table('cmf_rl_apply_delay')->where(['azd_id'=>$v['azd_id'], 'status'=>0])->limit(1)->count()){
                        unset($list[$k]);
                    }
                }
            }
            $user['delay_num']="N";
        }
        $this->success("查询成功!",['user'=>$user]);
    }

    //修改进行中安装单信息
    public function updateitemtwo()
    {

        $data = $this->request->param();
        $data_n = $data;
        $data_n['update_time'] = time();
        //所在地区
        $rlHiseAreaModel = new RlHiseAreaModel();
        $rlHiseDqTableModel = new RlHiseDqTableModel();
        $rlHiseQxTableModel = new RlHiseQxTableModel();
        $sf_name = $rlHiseAreaModel->where('sf_code',$data['provice'])->value('sf_name');
        $dq_name = $rlHiseDqTableModel->where('dq_code',$data['city'])->value('dq_name');
        $qx_name = $rlHiseQxTableModel->where('qx_code',$data['qx'])->value('qx_name');
        $data_n['azd_name'] = $sf_name.$dq_name.$qx_name.$data['address'];
        if(isset($data['jkh']) && $data['jkh']){
            if($data['jkh'] >= config('installsendstar') && $data['jkh'] <= config('installsendend'))
            {
                
            }elseif(substr($data['jkh'],0,3)=='HXT'&&strlen($data['jkh'])==9&&is_numeric(substr($data['jkh'],3))){
                if(substr($data['jkh'],3)>200000)
                {
                    $this->error('VIP卡号范围不正确');
                }
            }else{
                $this->error('VIP卡号格式不正确');
            }
        }
        $hise_service_project_id = $data['id'];
        $azd = $this->rlHiseServiceProjectModel->where('hise_service_project_id',$hise_service_project_id)->find()['azd'];
        $rusult = 0;
        if(isset($data['id']) && $data['id']){
            if(isset($data['azs'])&&$data['azs'])
            {
                $jg = $this->RlEtJgModel->where('server_code',$data['azs'])->where('qs_date','<=',date('H',time()))->where('jz_date','>=',date('H',time()))->find();
                if($jg)
                {
                    $data_n['sfjg'] = 1;
                }else{
                    $data_n['sfjg'] = 0;
                }
            }
            unset($data_n['id']);
            unset($data['id']);
                    $rusult = $this->rlHiseServiceProjectModel->where('hise_service_project_id',$hise_service_project_id)->update($data_n);
                    if($rusult)
                    {
                        $rusult = $hise_service_project_id;
                    }
        }
        if($rusult)
        {
        if(config('installsend1')&&isset($data['azd'])&&$data['azd']!=$azd)
          {
            //获取安装队手机号信息
            $phone = $this->rlEtAzdModel->where('engineer_code',$data['azd'])->find()['phone'];
            $azd_id = $this->rlHiseServiceProjectModel->where('hise_service_project_id',$hise_service_project_id)->find()['azd_id'];
            // 引用发送验证码类
                $msg_send = new MsgService();
                // 发送手机验证码
                $msgres = $msg_send->installsend1($azd_id,$phone);
                if($msgres)
                {
                    trace($phone.'||'.date('Y-m-d h:i:s',time()).'installsend1短信发送成功','info');
                }else{
                    trace($phone.'||'.date('Y-m-d h:i:s',time()).'installsend1短信发送失败','info');
                }
          }
            $this->success("保存成功!",['id'=>$rusult]);
        }else{
            $this->error("保存失败!");
        }
    }

}


